import { View } from "../../../mvc/core/View";
import { PageTool } from "../../../common/global/PageTool";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { TilePanel } from "../equipment/TilePanel";
import { IllustratePieceRenderer } from "./IllustratePieceRenderer";
import { PageSelecter } from "../common/pageSelect/PageSelecter";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { TIllustrateCollect } from "../../../../Message/Db/Tables/TIllustrateCollect";
import { IllustratePieceInfo } from "./IllustratePieceInfo";
import { IllustrateState } from "./IllustrateState";
import { Cache } from "../../cache/Cache";
import { ItemData } from "../../resource/info/ItemData";
import { TItemWeapon } from "../../../../Message/Db/Tables/TItemWeapon";
import { EColor } from "../../../../Message/Public/EColor";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
type int = number;
//class IllustratePieceList
    
    export  class IllustratePieceList extends View
    {
        constructor()
        {
            super();
            this._pieceInfoArr = [];
            this._pageTool = new PageTool([], 8);
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(155, 2, 170, 31, this, ImagesConst.SelectedBg);
            ObjCreate.createBg(0, 35, 470, 2, this, ImagesConst.SplitLine);
            var loc1=new GTextFormat("", 20, 16776960, true, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._nameTxt = ObjCreate.createGTextField("", 0, 5, 470, 30, this, loc1);
            this._listTile = new TilePanel();
            this._listTile.horizontalGap = 3;
            this._listTile.verticalGap = 5;
            this._listTile.columnWidth = 113;
            this._listTile.rowHeight = 170;
            this._listTile.columnCount = 4;
            this._listTile.rowCount = 2;
            this._listTile.setSize(480, 345);
            this._listTile.setStyle("cellRenderer", IllustratePieceRenderer);
            this._listTile.move(5, 40);
            addChild(this._listTile);
            this._pageSelect = new PageSelecter();
            this._pageSelect.mode = PageSelecter.CompleteMode;
            this._pageSelect.move(330, 10);
            addChild(this._pageSelect);
            this._pageSelect.addEventListener(flash.events.Event.CHANGE, this.onPageSelectChangeHandler);
            return;
        }

        /* internal  */onPageSelectChangeHandler(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=this._pageTool.getByPageNo(this._pageSelect.currentPage);
            this._listTile.dataProvider = new DataProvider(loc1);
            this._listTile.drawNow();
            return;
        }

        public updateList(arg1: Array<any>): void
        {
            if (arg1.length <= 8) 
            {
                this._pageSelect.visible = false;
            }
            else 
            {
                this._pageSelect.visible = true;
            }
            this._pageTool.array = arg1;
            this._pageSelect.maxPage = Math.ceil(arg1.length / 8);
            var loc1=this._pageTool.getByPageNo(this._pageSelect.currentPage);
            this._listTile.dataProvider = new DataProvider(loc1);
            this._listTile.drawNow();
            return;
        }

        public initAndUpdatePieceList(arg1: Array<any>, arg2: TIllustrateCollect): void
        {
            var loc1=null;
            var loc2=0;
            if (arg2 && !(this._illustrateCollect == arg2)) 
            {
                this._illustrateCollect = arg2;
                this._nameTxt.text = this._illustrateCollect.name;
                this._pieceInfoArr = [];
                loc2 = 0;
                while (loc2 < arg1.length) 
                {
                    if (arg1[loc2] != "") 
                    {
                        loc1 = new IllustratePieceInfo();
                        loc1.code = int(arg1[loc2]);
                        loc1.state = IllustrateState.IllustrateNotActive;
                        this._pieceInfoArr.push(loc1);
                    }
                    ++loc2;
                }
                this._pageSelect.currentPage = 1;
                this.updateList(this._pieceInfoArr);
                if (this._selectIdx != -1) 
                {
                    this.updateListSelectItem();
                }
                else 
                {
                    this._listTile.selectedIndex = 0;
                }
            }
            return;
        }

        public updateIllustratePieceState(): void
        {
            var loc1=null;
            var loc3=0;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc8=0;
            var loc9=0;
            var loc10=null;
            var loc11=0;
            if (this._illustrateCollect == null) 
            {
                return;
            }
            var loc2=Cache.instance.illustrate.getIllustrateActiveInfoById(this._illustrateCollect.id);
            if (loc2) 
            {
                loc4 = loc2.pieces;
                loc3 = 0;
                while (loc3 < this._pieceInfoArr.length) 
                {
                    loc6 = null;
                    loc7 = null;
                    loc1 = this._pieceInfoArr[loc3];
                    if (this.isActivePiecce(loc1, loc4)) 
                    {
                        if ((loc9 = ((loc5 = new ItemData(loc1.code)).itemInfo.extend as TItemWeapon).attackSpeed) != 0) 
                        {
                            loc11 = (loc10 = new ItemData(loc9)).itemInfo.item.codeUnbind;
                            loc6 = Cache.instance.pack.backPackCache.getItemByCode(loc9);
                            loc7 = Cache.instance.pack.backPackCache.getItemByCode(loc11);
                        }
                        if (loc6 && loc6.uid || loc7 && loc7.uid) 
                        {
                            if (loc5.itemInfo.item.color != EColor._EColorBlue) 
                            {
                                if (loc5.itemInfo.item.color == EColor._EColorPurple) 
                                {
                                    loc1.state = IllustrateState.IllustrateCanActiveOrange;
                                }
                            }
                            else 
                            {
                                loc1.state = IllustrateState.IllustrateCanActivePurple;
                            }
                            if (loc6 && loc6.uid) 
                            {
                                loc1.uid = loc6.uid;
                            }
                            else if (loc7 && loc7.uid) 
                            {
                                loc1.uid = loc7.uid;
                            }
                        }
                        else 
                        {
                            loc1.state = IllustrateState.IllustrateHadActive;
                        }
                    }
                    else 
                    {
                        loc8 = (loc5 = new ItemData(loc1.code)).itemInfo.item.codeUnbind;
                        loc6 = Cache.instance.pack.backPackCache.getItemByCode(loc1.code);
                        loc7 = Cache.instance.pack.backPackCache.getItemByCode(loc8);
                        if (loc6 && loc6.uid) 
                        {
                            loc1.state = IllustrateState.IllustrateCanActiveBlue;
                            loc1.uid = loc6.uid;
                        }
                        else if (loc7 && loc7.uid) 
                        {
                            loc1.state = IllustrateState.IllustrateCanActiveBlue;
                            loc1.uid = loc7.uid;
                        }
                        else 
                        {
                            loc1.state = IllustrateState.IllustrateNotActive;
                        }
                    }
                    ++loc3;
                }
            }
            this.updateList(this._pieceInfoArr);
            if (this._selectIdx != -1) 
            {
                this.updateListSelectItem();
                this._selectIdx = -1;
            }
            return;
        }

        /* internal  */isActivePiecce(arg1: IllustratePieceInfo, arg2: Map<any, any> /* flash.utils.Dictionary */): Boolean
        {
            var loc4=null;
            var loc5=0;
            var loc1=arg1.code;
            var loc2=false;
            var loc3=new ItemData(loc1);
            var loc6=0;
            var loc7=arg2;
            for(loc5 of loc7) 
            {
                loc4 = new ItemData(loc5);
                if ((loc3.itemInfo.extend as TItemWeapon).attackDistance != (loc4.itemInfo.extend as TItemWeapon).attackDistance) 
                {
                    continue;
                }
                loc2 = true;
                arg1.code = loc5;
            }
            return loc2;
        }

        public setSelectedItem(arg1: int, arg2: int): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=0;
            var loc4=0;
            while (loc4 < this._pieceInfoArr.length) 
            {
                if (loc2 = this._pieceInfoArr[loc4]) 
                {
                    if (((loc3 = new ItemData(loc2.code)).itemInfo.extend as TItemWeapon).attackDistance == arg2) 
                    {
                        loc1 = loc4;
                        break;
                    }
                }
                ++loc4;
            }
            this._selectIdx = loc1;
            this.updateListSelectItem();
            return;
        }

        /* internal  */updateListSelectItem(): void
        {
            var loc1=this._selectIdx;
            if (loc1 >= this._pageTool.limit) 
            {
                this._pageSelect.currentPage = Math.ceil((loc1 + 1) / this._pageTool.limit);
                this.onPageSelectChangeHandler();
                loc1 = loc1 % this._pageTool.limit;
            }
            else 
            {
                this._pageSelect.currentPage = 1;
                this.onPageSelectChangeHandler();
            }
            this._listTile.selectedIndex = loc1;
            return;
        }

        /* internal */ /* var */_listTile: TilePanel;

        /* internal */ /* var */_illustrateCollect: TIllustrateCollect;

        /* internal */ /* var */_pieceInfoArr: Array<any>;

        /* internal */ /* var */_nameTxt: GTextFiled;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_pageSelect: PageSelecter;

        /* internal */ /* var */_selectIdx: int=-1;
    }
